import { Controller } from "../../mvc/core/Controller";
import { ChristmasIcon } from "../view/activity/christmas/ChristmasIcon";
import { ClockManager } from "../manager/ClockManager";
import { IChristmasActivitiesModule } from "../../../modules/interfaces/IChristmasActivitiesModule";
import { DataEvent } from "../events/DataEvent";
import { ChristmasActivitiesInfo } from "../model/ChristmasActivitiesInfo";
import { AIManager } from "../scene/ai/AIManager";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DailyRule } from "../rules/DailyRule";
import { GameController } from "../mvc/GameController";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { WindowEvent } from "../../component/window/WindowEvent";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { IView } from "../../mvc/interfaces/IView";
import { ChristmasActivitiesModule } from "../../../modules/ChristmasActivitiesModule";
//class ChristmasActivitiesController
    
    export  class ChristmasActivitiesController extends Controller
    {
       

        public isIconShow(): Boolean
        {
            return this._christmasIcon && !this._christmasIcon.isHide;
        }

        public get christamsIcon(): ChristmasIcon
        {
            if (!this._christmasIcon) 
            {
                this._christmasIcon = new ChristmasIcon();
            }
            return this._christmasIcon;
        }

        public activeTimeBegin(): void
        {
            if (!this.isIconShow()) 
            {
                this.christamsIcon.show();
            }
            if (cache.christmasActive.nextIconInfo) 
            {
                ClockManager.instance.removeDateCall(cache.christmasActive.nextIconInfo.timeDate);
            }
            cache.christmasActive.refreshActiveIconRule();
            this.christamsIcon.updateIconInfo(cache.christmasActive.curIconInfo);
            if (_view) 
            {
                (_view as IChristmasActivitiesModule).updateIconInfo(cache.christmasActive.curIconInfo);
            }
            if (cache.christmasActive.nextIconInfo) 
            {
                ClockManager.instance.addDateCall(cache.christmasActive.nextIconInfo.timeDate, this.activeTimeBegin);
            }
            return;
        }

        /* internal  */onChristmasOptReq(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=arg1.data as ChristmasActivitiesInfo;
            if (!loc1) 
            {
                return;
            }
            var loc4=loc1.type;
            switch (loc4) 
            {
                case 1: case 2:
                {
                    if (loc1.targetInfo) 
                    {
                        AIManager.onAutoPathAIControl(loc1.targetInfo);
                    }
                    break;
                }
                case 3:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGiftOpen, DailyRule.GiftGet));
                    break;
                }
                case 4:
                {
                    break;
                }
                case 5:
                {
                    GameController.daily.view.show();
                    break;
                }
                case 6:
                {
                    loc2 = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropFirework);
                    if (loc2 && loc2.length > 0) 
                    {
                        loc3 = loc2[0];
                        GameController.pack.view.show();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.BackPackCodeItemPlayEffect, loc3.itemCode));
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(21010));
                    }
                    break;
                }
                case 7:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltOpenSmeltByIndex, 5));
                    break;
                }
                case 8:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PeaceFieldShowEnterPanel));
                    break;
                }
                case 9:
                {
                    GameController.shopMall.view.show();
                    break;
                }
                case 10:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGiftOpen, DailyRule.SpecialActive));
                    break;
                }
                case 11:
                {
                    GameController.vip.view.show();
                    break;
                }
                case 12:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGiftOpen, DailyRule.WelfareGet));
                    break;
                }
            }
            return;
        }

        /* internal  */onServerOpenDayRes(arg1: DataEvent): void
        {
            if (cache.christmasActive.beforeChristmasTime()) 
            {
                ClockManager.instance.addDateCall(cache.christmasActive.startDate, this.onChristmasBegin);
            }
            else if (!cache.christmasActive.afterChristmasTime()) 
            {
                ClockManager.instance.addDateCall(cache.christmasActive.endDate, this.onChristmasEnd);
                this.activeTimeBegin();
            }
            return;
        }

        /* internal  */onChristmasBegin(): void
        {
            ClockManager.instance.addDateCall(cache.christmasActive.endDate, this.onChristmasEnd);
            this.activeTimeBegin();
            return;
        }

        /* internal  */onChristmasEnd(): void
        {
            if (cache.christmasActive.nextIconInfo) 
            {
                ClockManager.instance.removeDateCall(cache.christmasActive.nextIconInfo.timeDate);
            }
            if (this.isIconShow()) 
            {
                this.christamsIcon.hide();
            }
            if (isViewShow) 
            {
                view.hide();
            }
            return;
        }

        public updateList(): void
        {
            if (cache.christmasActive.nextRefreshDate) 
            {
                ClockManager.instance.removeDateCall(cache.christmasActive.nextRefreshDate);
            }
            cache.christmasActive.updateActiveList();
            if (cache.christmasActive.nextRefreshDate != null) 
            {
                ClockManager.instance.addDateCall(cache.christmasActive.nextRefreshDate, this.refreshList);
            }
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */refreshList(): void
        {
            if (cache.christmasActive.nextRefreshDate) 
            {
                ClockManager.instance.removeDateCall(cache.christmasActive.nextRefreshDate);
            }
            cache.christmasActive.updateActiveList();
            if (cache.christmasActive.nextRefreshDate != null) 
            {
                ClockManager.instance.addDateCall(cache.christmasActive.nextRefreshDate, this.refreshList);
            }
            if (_view && !_view.isHide) 
            {
                (view as IChristmasActivitiesModule).updateList(cache.christmasActive.activeList);
            }
            return;
        }

        /* internal  */onChristmasShowHandler(arg1: WindowEvent): void
        {
            (view as IChristmasActivitiesModule).updateList(cache.christmasActive.activeList);
            return;
        }

        /* internal  */onActiveTimeUpdate(arg1: DataEvent): void
        {
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeShowActiveIcon)) 
            {
                this.activeTimeBegin();
            }
            else 
            {
                this.onChristmasEnd();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.ChristmasOptReq, this.onChristmasOptReq);
            Dispatcher.addEventListener(EventName.AcitveTimeUpdate, this.onActiveTimeUpdate);
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new ChristmasActivitiesModule();
            loc1.addEventListener(WindowEvent.SHOW, this.onChristmasShowHandler);
            return loc1;
        }

        private /* var */_christmasIcon: ChristmasIcon;
    }
